var ordersApp=angular.module("merchants",[])
    .controller("merchantInfoController",function ($scope,$http,$rootScope,merchantService,newOrders) {
        $rootScope.active = 'home';
        newOrders.newOrders();
        if(!$rootScope.permission){
            window.location = 'login.html'
        }


    })
    .controller("merchantUpdateController",function ($scope,$http,$rootScope,merchantService,Upload,newOrders) {
        newOrders.newOrders();
        if(!$rootScope.permission){
            window.location = 'login.html'
        }

        $rootScope.active = 'updateInfo';
        $scope.update = function(){
            // upload on file select or drop
            var file = $scope.file
            Upload.upload({
                url: '/merchants/updateInfo',
                method: "put",
                data:{_id:$rootScope.merchant._id,
                    mname:$rootScope.merchant.mname,
                    mtel:$rootScope.merchant.mtel,
                    password:$rootScope.merchant.password,
                    address:$rootScope.merchant.address,
                    type:$rootScope.merchant.type,
                    file:file},
            }).then(function (resp) {
                //file.result = resp.data;
                $rootScope.merchant = resp.data.merchant;
                window.location = '#/home'
            }, function (resp) {
                console.log('Error status: ' + resp.status);
            }, function (evt) {
                //file.progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
            });
        }
    })


var loginApp=angular.module("loginApp",['ngFileUpload'])
    .controller("merchantLoginController",function ($scope,$http,$rootScope) {
        $scope.login = null;

        $scope.Login = function(){
            var req = {
                method:'post',
                url:'merchants/login',
                data:$scope.login,
                headers:{"Content-type":"application/json"}
            }
            $http(req).then(function (res) {

                if(res.data.msg == 'SUCCESS'){
                    $rootScope.merchant = res.data.merchant;
                    window.location = "/#/home";
                }else{
                    $scope.loginError = res.data.msg
                    $scope.exist = true
                    console.log('error')
                }
            });
        }
    })
    .controller('merchantRegistController',['$scope', 'Upload', '$http', function ($scope, Upload,$http) {
        // upload later on form submit or something similar
        $scope.registerSubmit = function () {
            var file = $scope.file
            var mname = $scope.mname
            var mtel = $scope.mtel
            var password = $scope.password
            var type = $scope.type
            var address = $scope.address
            $scope.upload(file,mname,mtel,password,type,address);
        };

        // upload on file select or drop
        $scope.upload = function (file,mname,mtel,password,type,address) {
            Upload.upload({
                url: '/merchants/regist',
                method: "POST",
                data:{mname:mname,mtel:mtel,password:password,address:address,type:type,file:file},
                file: file
            }).then(function (resp) {
                file.result = resp.data;
                window.location = 'login.html'
            }, function (resp) {
                console.log('Error status: ' + resp.status);
            }, function (evt) {
                file.progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                console.log('progress: ' + file.progressPercentage + '% ' + evt.config.file.name);
            });
        }
        
        $scope.checkUserName = function () {
            var req = {
                method:'POST',
                url:'/merchants/checkName',
                data:{mname:$scope.mname}
                //headers:{"Content-type":"application/json"}
            }
            $http(req).then(function (res) {
                if(res.data.msg) {
                    $scope.nameError = "User name exist!";
                }
                else {
                    $scope.nameError = '';
                }
            });
        }
    }])
